package com.caigou.modules.forum.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.PrizeBallBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.forum.bean.FifaItemBean
import com.caigou.modules.forum.repository.ForumRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/17
 */
@HiltViewModel
class PublishViewModel @Inject constructor(private val repository: ForumRepository) : ViewModel() {

    val publishPostLiveData = MutableLiveData<Resource<String>>()
    val prizeBallListLiveData = MutableLiveData<Resource<MutableList<PrizeBallBean>>>()
    val fifaTypeListLiveData = MutableLiveData<Resource<MutableList<FifaItemBean>>>()

    fun publishPost(files: MutableList<File>, title: String, content: String, userId: String, dataType: Int, redPacketMoney: Float, redPacketCount: Int) {
        publishPostLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.publishPost(files, title, content, userId, dataType, redPacketMoney, redPacketCount)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    publishPostLiveData.value = it
                }
        }
    }

    fun publishContestPost(
        files: MutableList<File>, title: String, content: String, userId: String,
        dataType: Int, dareContent: String, dareType: Int, redPacketMoney: Float, redPacketCount: Int
    ) {
        publishPostLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.publishContestPost(files, title, content, userId, dataType, dareContent, dareType, redPacketMoney, redPacketCount)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    publishPostLiveData.value = it
                }
        }
    }

    fun publishFifaPost(
        files: MutableList<File>, title: String, content: String,
        userId: String, dataType: Int, fifaType: Int,
        redPacketMoney: Float, redPacketCount: Int
    ) {
        publishPostLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.publishFifaPost(files, title, content, userId, dataType, fifaType, redPacketMoney, redPacketCount)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    publishPostLiveData.value = it
                }
        }
    }

    fun publishMarketPost(
        files: MutableList<File>, title: String, content: String, userId: String,
        dataType: Int, superMaterialType: Int, redPacketMoney: Float, redPacketCount: Int
    ) {
        publishPostLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.publishMarketPost(files, title, content, userId, dataType, superMaterialType, redPacketMoney, redPacketCount)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    publishPostLiveData.value = it
                }
        }
    }


    fun getPrizeBallList() {
        prizeBallListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getPrizeBallList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<PrizeBallBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    prizeBallListLiveData.value = it
                }
        }
    }

    fun getFifaTypeList() {
        fifaTypeListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFifaTypeList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<FifaItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    fifaTypeListLiveData.value = it
                }
        }
    }

}